package com.example.kotlinview.flow

import com.example.utils.lineStar
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

fun main()  {
    val launch = GlobalScope.launch {
        flow {
            emit(getData())
        }.flowOn(Dispatchers.IO) //切换线程
            .onStart {
                println("onStart")
            }.catch {
                println("catch:${it.message}")//有异常会进入此方法
            }.onCompletion {
                println("oniComplete:${it?.message}")//无论是否有异常都会执行
            }.collect {//收集流
                println("result = $it")
            }
    }
    Thread.sleep(6000)

}



private fun getData(): String {
    Thread.sleep(3000)
    return "Flow test"
}
